package com.haoze.nurseapp.schedule;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haoze.common.model.TaskExecuteBatchEntity;
import com.haoze.common.utils.DateUtil;
import com.haoze.nurseapp.dao.NurseFileIndexDataMapper;
import com.haoze.nurseapp.dao.PatientTemperatureScheduleMapper;
import com.haoze.nurseapp.dao.TaskExecuteDetailMapper;
import com.haoze.nurseapp.dao.TransfusionMapper;
import com.haoze.nurseapp.dto.PatientTemperatureScheduleDTO;
import com.haoze.nurseapp.enumeration.WorkSpaceEnum;
import com.haoze.nurseapp.model.PatientTemperatureScheduleEntity;
import com.haoze.nurseapp.model.TaskExecuteDetailEntity;
import com.haoze.nurseapp.service.PatientTemperatureService;
import com.haoze.nurseapp.service.TransfusionService;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@ConditionalOnProperty(prefix = "schedule", name = "copyData", havingValue = "true")
public class CheckEndTimeSchedule {

    @Resource
    NurseFileIndexDataMapper fileIndexDataMapper;

    @Resource
    TransfusionService transfusionService;

    @Resource
    TaskExecuteDetailMapper taskExecuteDetailMapper;

    @Resource
    TransfusionMapper transfusionMapper;

    @Resource
    PatientTemperatureScheduleMapper patientTemperatureScheduleMapper;

    @Resource
    PatientTemperatureService patientTemperatureService;

//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void checkEndTime(){
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String varCode = "sy_tqtxsj";
//        String varValue = transfusionMapper.queryVarvalue(varCode);
//        //按分钟扫描任务明细表获取正在执行输液且输液未结束的任务
//        List<TaskExecuteBatchEntity> taskExecuteDetailEntityList = taskExecuteDetailMapper.queryDataEntities(WorkSpaceEnum.INFUSION_CENTER.getNodeId());
//        List<Map<String,Object>> list = new ArrayList<>();
//        for (int i = 0; i < taskExecuteDetailEntityList.size(); i++) {
//            Map<String,Object> map = new HashMap<>();
//            Map<String,Object> resultMap = transfusionService.endInfusionInfo(taskExecuteDetailEntityList.get(i).getEmrFileId());
//            if(resultMap.size() > 0){
//                map.put("emrFileId",taskExecuteDetailEntityList.get(i).getEmrFileId());
//                map.put("endTime",resultMap.get("endTime"));
//                list.add(map);
//            }
//        }
//        if(list.size() > 0){
//            for (int i = 0; i < list.size(); i++) {
//                Map<String,Object> map = list.get(i);
//                String emrFileId = (String) map.get("emrFileId");
//                //根据emrFileId获取提前提醒时间数据元数据
//                Map<String,String> remindMap = fileIndexDataMapper.queryString(emrFileId,varValue);
//                Date endTime = (Date) map.get("endTime");
//                long remindSecs = endTime.getTime() - (Integer.parseInt(remindMap.get("remindTime"))*60*1000);
//                Date remindDate = new Date(remindSecs);
//                String currentTime = sdf.format(date);
//                String remindTime = sdf.format(remindDate);
//                if(currentTime.equals(remindTime)){
//                    String str = remindMap.get("bedNo") + "床病人输液还有" + remindMap.get("remindTime") + "分结束！";
//                }
//            }
//        }
//    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void temperatureSchedule(){
        List<PatientTemperatureScheduleDTO> list =  patientTemperatureScheduleMapper.selectSchedule();
        for(PatientTemperatureScheduleDTO dto : list){
            JSONObject object = JSON.parseObject(dto.getDataJson());
            String twlxCode = object.get("twlxCode").toString();
            String twlxText = object.get("twlxText").toString();
            String tw = object.get("tw").toString();
            String mb = object.get("mb").toString();
            String hx = object.get("hx").toString();
            String szy = object.get("szy").toString();
            String ssy = object.get("ssy").toString();
            String userName = object.get("userName").toString();
            String inpatNum = object.get("inpatNum").toString();
            String bedNo = object.get("bedNo").toString();
            Date date = DateUtil.stringToDate(object.get("date").toString(),"yyyy-MM-dd HH:mm");
            patientTemperatureService.nurseFileSave(twlxCode,twlxText,tw,mb,hx,szy,ssy,inpatNum,bedNo,date,userName);
            dto.setStatus("1");
            PatientTemperatureScheduleEntity entity = new PatientTemperatureScheduleEntity();
            BeanUtils.copyProperties(dto,entity);
            patientTemperatureScheduleMapper.updateByPrimaryKey(entity);
        }

    }

}
